//
//  UIView+Extension.swift

import UIKit
import ObjectiveC
extension UIViewController {
    
    /// 状态栏高度
    var ksStatusbarHeight:CGFloat {        
        let statusBarManager = UIApplication.shared.windows.first?.windowScene?.statusBarManager
        return statusBarManager?.statusBarFrame.size.height ?? 20.0
    }
    /// 导航栏高度
    var ksNavigationbarHeight:CGFloat {
        return self.navigationController?.navigationBar.frame.size.height ?? 0
    }
    /// 底部tabbar高度
    var ksTabbarHeight:CGFloat {
        return self.tabBarController?.tabBar.bounds.size.height ?? 0
    }
    
}
// 把切换图标 切换logo 的弹窗 沉默掉
extension UIViewController{
    public class func initializeMethod(){
        if self != UIViewController.self{
            return
        }
        DispatchQueue.once(token: "ChangeIcon") {
            let orignal = class_getInstanceMethod(self, #selector(UIViewController.present(_:animated:completion:)))
            let swizzling = class_getInstanceMethod(self, #selector(UIViewController.ssl_present(_:animated:completion:)))
            if let old = orignal, let new = swizzling{
                method_exchangeImplementations(old, new)
            }
        }
    }
    @objc private func ssl_present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil) {
        if viewControllerToPresent is UIAlertController{
            let vc = viewControllerToPresent as! UIAlertController
            if vc.title == nil && vc.message == nil{
                return
            }
        }
        self.ssl_present(viewControllerToPresent, animated: flag, completion: completion)
    }
}
extension DispatchQueue{
    private static var _onceTracker = [String]()
    public class func once(token: String, block:()->()){
        objc_sync_enter(self)
        defer{
            objc_sync_exit(self)
        }
        if _onceTracker.contains(token){
            return
        }
        _onceTracker.append(token)
        block()
    }
}



extension UITableView {
    // 静态变量，用于标记方法是否已经交换
    private static var hasSwizzledInit = false
    
    @objc private func customInit(frame: CGRect, style: UITableView.Style) -> Self {
        // 调用原始的 init 方法
        let tableView = self.customInit(frame: frame, style: style)
        if #available(iOS 15.0, *) {
            tableView.sectionHeaderTopPadding = 0
        }
        return tableView
    }
    
    static func swizzleInitMethods() {
        // 避免重复交换
        guard !hasSwizzledInit else { return }
        hasSwizzledInit = true
        
        // 获取原始的 init 方法
        let originalSelector = #selector(UITableView.init(frame:style:))
        let originalMethod = class_getInstanceMethod(UITableView.self, originalSelector)
        
        // 获取自定义的 init 方法
        let swizzledSelector = #selector(UITableView.customInit(frame:style:))
        let swizzledMethod = class_getInstanceMethod(UITableView.self, swizzledSelector)
        
        // 添加自定义方法，如果原始方法不存在
        if let originalMethod = originalMethod, let swizzledMethod = swizzledMethod {
            let didAddMethod = class_addMethod(UITableView.self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))
            if didAddMethod {
                class_replaceMethod(UITableView.self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
            } else {
                method_exchangeImplementations(originalMethod, swizzledMethod)
            }
        }
    }
}
 
